package com.example.controller;

import com.example.common.RequirePermission;
import com.example.common.Result;
import com.example.common.PageResult;
import com.example.dto.HaircutPackageDTO;
import com.example.service.HaircutPackageService;
import com.example.vo.HaircutPackageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 理发套餐控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/haircut-package")
@CrossOrigin
public class HaircutPackageController {
    
    @Autowired
    private HaircutPackageService haircutPackageService;
    
    /**
     * 新增套餐
     */
    @PostMapping
    @RequirePermission("store:package:add")
    @com.example.common.OperationLog(
        module = "套餐管理",
        operationType = "CREATE",
        description = "新增理发套餐",
        recordParams = true,
        businessType = "HAIRCUT_PACKAGE"
    )
    public Result<Void> addHaircutPackage(@RequestBody HaircutPackageDTO packageDTO) {
        try {
            return haircutPackageService.addHaircutPackage(packageDTO);
        } catch (Exception e) {
            log.error("新增理发套餐失败", e);
            return Result.error("新增套餐失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除套餐
     */
    @DeleteMapping("/{id}")
    @RequirePermission("store:package:delete")
    @com.example.common.OperationLog(
        module = "套餐管理",
        operationType = "DELETE",
        description = "删除理发套餐",
        businessType = "HAIRCUT_PACKAGE"
    )
    public Result<Void> deleteHaircutPackage(@PathVariable Long id) {
        try {
            return haircutPackageService.deleteHaircutPackage(id);
        } catch (Exception e) {
            log.error("删除理发套餐失败", e);
            return Result.error("删除套餐失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新套餐
     */
    @PutMapping("/{id}")
    @RequirePermission("store:package:update")
    @com.example.common.OperationLog(
        module = "套餐管理",
        operationType = "UPDATE",
        description = "更新理发套餐",
        recordParams = true,
        businessType = "HAIRCUT_PACKAGE"
    )
    public Result<Void> updateHaircutPackage(@PathVariable Long id, @RequestBody HaircutPackageDTO packageDTO) {
        try {
            return haircutPackageService.updateHaircutPackage(id, packageDTO);
        } catch (Exception e) {
            log.error("更新理发套餐失败", e);
            return Result.error("更新套餐失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据ID查询套餐
     */
    @GetMapping("/{id}")
    @RequirePermission("store:package:view")
    public Result<HaircutPackageVO> getHaircutPackageById(@PathVariable Long id) {
        try {
            return haircutPackageService.getHaircutPackageById(id);
        } catch (Exception e) {
            log.error("查询理发套餐失败", e);
            return Result.error("查询套餐失败：" + e.getMessage());
        }
    }
    
    /**
     * 查询所有套餐
     */
    @GetMapping("/list")
    @RequirePermission("store:package:list")
    public Result<List<HaircutPackageVO>> getAllHaircutPackages() {
        try {
            return haircutPackageService.getAllHaircutPackages();
        } catch (Exception e) {
            log.error("查询所有理发套餐失败", e);
            return Result.error("查询套餐失败：" + e.getMessage());
        }
    }
    
    /**
     * 查询所有启用的套餐
     */
    @GetMapping("/enabled")
    public Result<List<HaircutPackageVO>> getAllEnabledHaircutPackages() {
        try {
            return haircutPackageService.getAllEnabledHaircutPackages();
        } catch (Exception e) {
            log.error("查询启用的理发套餐失败", e);
            return Result.error("查询套餐失败：" + e.getMessage());
        }
    }
    
    /**
     * 分页查询套餐
     */
    @GetMapping("/page")
    @RequirePermission("store:package:list")
    public Result<PageResult<HaircutPackageVO>> getHaircutPackagePage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String keyword) {
        try {
            return haircutPackageService.getHaircutPackagePage(page, size, keyword);
        } catch (Exception e) {
            log.error("分页查询理发套餐失败", e);
            return Result.error("查询套餐失败：" + e.getMessage());
        }
    }
    
    /**
     * 检查套餐名称是否存在
     */
    @GetMapping("/check-name")
    @RequirePermission("store:package:view")
    public Result<Boolean> checkPackageNameExists(
            @RequestParam String packageName,
            @RequestParam(required = false) Long excludeId) {
        try {
            return haircutPackageService.checkPackageNameExists(packageName, excludeId);
        } catch (Exception e) {
            log.error("检查套餐名称是否存在失败", e);
            return Result.error("检查失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新套餐状态
     */
    @PutMapping("/{id}/status")
    @RequirePermission("store:package:update")
    @com.example.common.OperationLog(
        module = "套餐管理",
        operationType = "UPDATE",
        description = "更新理发套餐状态",
        businessType = "HAIRCUT_PACKAGE"
    )
    public Result<Void> updatePackageStatus(@PathVariable Long id, @RequestParam Integer status) {
        try {
            return haircutPackageService.updatePackageStatus(id, status);
        } catch (Exception e) {
            log.error("更新理发套餐状态失败", e);
            return Result.error("更新状态失败：" + e.getMessage());
        }
    }
    
    /**
     * 启用套餐
     */
    @PutMapping("/{id}/enable")
    @RequirePermission("store:package:update")
    @com.example.common.OperationLog(
        module = "套餐管理",
        operationType = "UPDATE",
        description = "启用理发套餐",
        businessType = "HAIRCUT_PACKAGE"
    )
    public Result<Void> enablePackage(@PathVariable Long id) {
        try {
            return haircutPackageService.enablePackage(id);
        } catch (Exception e) {
            log.error("启用理发套餐失败", e);
            return Result.error("启用套餐失败：" + e.getMessage());
        }
    }
    
    /**
     * 禁用套餐
     */
    @PutMapping("/{id}/disable")
    @RequirePermission("store:package:update")
    @com.example.common.OperationLog(
        module = "套餐管理",
        operationType = "UPDATE",
        description = "禁用理发套餐",
        businessType = "HAIRCUT_PACKAGE"
    )
    public Result<Void> disablePackage(@PathVariable Long id) {
        try {
            return haircutPackageService.disablePackage(id);
        } catch (Exception e) {
            log.error("禁用理发套餐失败", e);
            return Result.error("禁用套餐失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取套餐统计信息
     */
    @GetMapping("/statistics")
    @RequirePermission("store:package:view")
    public Result<Object> getPackageStatistics() {
        try {
            return haircutPackageService.getPackageStatistics();
        } catch (Exception e) {
            log.error("获取套餐统计信息失败", e);
            return Result.error("获取统计信息失败：" + e.getMessage());
        }
    }
} 